import { Base } from "./base.js";
import { unescape, autoParenter } from "./helpers.js";

export class Package extends Base {
  static parse(tokeniser) {
    const packageKeyword = tokeniser.consume("package");
    if (!packageKeyword) {
      return;
    }
    const tokens = { base: packageKeyword };
    tokens.name =
      tokeniser.consumeKind("string") || tokeniser.error("No name for package");
    tokens.termination =
      tokeniser.consume(";") || tokeniser.error("No semicolon after package");
    const ret = autoParenter(new Package({ source: tokeniser.source, tokens }));
    ret.nameValue = tokens.name.value;
    return ret.this;
  }

  get type() {
    return "package";
  }
  get name() {
    return unescape(this.tokens.name.value);
  }

  write(w) {
    return w.ts.definition(
      w.ts.wrap([
        w.token(this.tokens.base),
        w.reference_token(this.tokens.name, this),
        w.token(this.tokens.termination),
      ]),
      { data: this },
    );
  }
}
